package test.lyj.tool;

import android.util.Log;
import json.CurveObj;
import json.ListMyCurvesObj;
import json.NormalObject;
import json.PointObj;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class Jsonformat2Bean {
    public static Object formatJsonStr2Bean(String jsonStr)
            throws JSONException {
        JSONObject ob = new JSONObject(jsonStr);
        // Tool.sho

        if (ob.has("NormalObj")) {
            return initNormalObj(ob.getJSONObject("NormalObj"));
        }
        if (ob.has("ListMyCurvesObj")) {
            return listMyCurvesObj(ob.getJSONObject("ListMyCurvesObj"));
        }
        if (ob.has("CurveObj")) {
            return initCurveObj(ob.getJSONObject("CurveObj"));
        }

        return null;
    }

    private static ListMyCurvesObj listMyCurvesObj(JSONObject ob) throws JSONException {
        ListMyCurvesObj c = new ListMyCurvesObj();
        if (ob.has("flagNum")) {
            int d = ob.getInt("flagNum");
            c.setFlagNum(d);
        }
        if (ob.has("msg")) {
            String d = ob.getString("msg");
            c.setMsg(d);
        }
        if (ob.has("curveArray")) {
            ArrayList<CurveObj> listOB = new ArrayList<CurveObj>();
            JSONArray listOb2 = ob.getJSONArray("curveArray");
            for (int j = 0; j < listOb2.length(); j++) {
                JSONObject detailOb2 = listOb2.getJSONObject(j);
                if (Tool.DEBUG)
                    Log.d("detailcurve==", String.valueOf(detailOb2.toString()));
                listOB.add(initCurveObj(detailOb2));
            }
            c.setCurveArrayByList(listOB);
        }
        return c;
    }

    private static CurveObj initCurveObj(JSONObject ob) throws JSONException {
        CurveObj c = new CurveObj();
        if (ob.has("sysId")) {
            c.setSysId(ob.getString("sysId"));
        }
        if (ob.has("createdateStr")) {
            c.setCreatedateStr(ob.getString("createdateStr"));
        }
        if (ob.has("title")) {
            c.setTitle(ob.getString("title"));
        }
        if (ob.has("createDate")) {
            c.setCreateDate(ob.getLong("createDate"));
        }
        if (ob.has("pointArray")) {
            ArrayList<PointObj> listOB = new ArrayList<PointObj>();
            JSONArray listOb2 = ob.getJSONArray("pointArray");
            for (int j = 0; j < listOb2.length(); j++) {
                JSONObject detailOb2 = listOb2.getJSONObject(j);
                if (Tool.DEBUG)
                    Log.d("detailpoint==", String.valueOf(detailOb2.toString()));
                listOB.add(initPointObj(detailOb2));
            }
            c.setPointArrayByList(listOB);
        }


        return c;
    }

    private static PointObj initPointObj(JSONObject ob) throws JSONException {
        PointObj c =new PointObj();
        if (ob.has("startDate")) {
            c.setStartDate(ob.getLong("startDate"));
        }
        if (ob.has("createDate")) {
            c.setStartDate(ob.getLong("createDate"));
        }
        if (ob.has("followDays")) {
            c.setFollowDays(ob.getInt("followDays"));
        }
        if (ob.has("yvalue")) {
            c.setYvalue((float)ob.getDouble("yvalue"));
        }
        return c;
    }

    private static NormalObject initNormalObj(JSONObject ob)
            throws JSONException {
        return new NormalObject(ob.getInt("flagNum"), ob
                .getString("msg"));
    }

}
